package com.sjs.boot.dynamic.util;

import java.util.*;

/**
 * @author uto_dev_008
 * @desc
 * @since 2021/11/9 18:25
 */
public class ValParser {
    /**
     * 将一个对象转为整形数
     * @param o
     * @param defVal 如果解析失败时，返回defVal
     * @return
     */
    public static int parseInt(Object o, int defVal) {
        if (o == null) {
            return defVal;
        }

        if (o instanceof Number) {
            return ((Number) o).intValue();
        }

        if (o instanceof String) {
            try {
                return Integer.parseInt((String) o);
            } catch (Exception e) {
            }
        }

        return defVal;
    }

    /**
     * 将一个byte数组转为整型数
     * @param b
     * @param offset
     * @param len
     * @return
     */
    public static int parseInt(byte[] b, int offset, int len) {
        if (b.length < offset + len || len > 4) {
            throw new NumberFormatException("Wrong int format");
        }

        int v = 0;
        for (int i = 0; i < len; i++) {
            v <<= 8;
            v |= ((int) b[i + offset]) & 0xff;
        }
        return v;
    }

    /**
     * 将byte数组转为整型
     * @param b
     * @return
     */
    public static int parseInt(byte[] b) {
        int v = 0;
        for (int i = 0; i < 4; i++) {
            v <<= 8;
            v |= ((int) b[i]) & 0xff;
        }
        return v;
    }

    public static boolean parseInt(int v, byte[] b, int offset) {
        if (b.length < offset + 4) {
            return false;
        }

        for (int i = 0; i < 4; i++) {
            b[offset++] = (byte) ((v >> ((3 - i) << 3)) & 0xff);
        }
        return true;
    }

    public static float parseFloat(Object o, float defVal) {
        if (o == null) {
            return defVal;
        }

        if (o instanceof Number) {
            return ((Number) o).floatValue();
        }

        if (o instanceof String) {
            try {
                return Float.parseFloat((String) o);
            } catch (Exception e) {
            }
        }

        return defVal;
    }

    public static double parseDouble(Object o, double defVal) {
        if (o == null) {
            return defVal;
        }

        if (o instanceof Number) {
            return ((Number) o).doubleValue();
        }

        if (o instanceof String) {
            try {
                return Double.parseDouble((String) o);
            } catch (Exception e) {
            }
        }

        return defVal;
    }

    public static boolean parseBool(Object o, boolean defVal) {
        if (o == null) {
            return defVal;
        }

        if (o instanceof Boolean) {
            return ((Boolean) o).booleanValue();
        }

        if (o instanceof Number) {
            return (((Number) o).intValue() != 0);
        }

        if (o instanceof String) {
            return Boolean.parseBoolean((String) o);
        }

        return defVal;
    }

    public static long parseLong(Object o, long defVal) {
        if (o == null) {
            return defVal;
        }

        if (o instanceof Number) {
            return ((Number) o).longValue();
        }

        if (o instanceof String) {
            try {
                return Long.parseLong((String) o);
            } catch (Exception e) {
            }
        }

        return defVal;
    }

    public static String parseString(Object o, String defVal) {
        if (o == null) {
            return defVal;
        }

        return o.toString();
    }

    public static String getAsStr(Map<String, Object> map, String name) {
        return parseString(map.get(name), "");
    }

    public static String getAsStr(Map<String, Object> map, String name, String defVal) {
        return parseString(map.get(name), defVal);
    }

    public static int getAsInt(Map<String, Object> map, String name) {
        return parseInt(map.get(name), 0);
    }

    public static int getAsInt(Map<String, Object> map, String name, int def) {
        return parseInt(map.get(name), def);
    }

    public static long getAsLong(Map<String, Object> map, String name) {
        return parseLong(map.get(name), 0L);
    }

    public static long getAsLong(Map<String, Object> map, String name, long defVal) {
        return parseLong(map.get(name), defVal);
    }

    public static float getAsFloat(Map<String, Object> map, String name) {
        return parseFloat(map.get(name), 0f);
    }

    public static float getAsFloat(Map<String, Object> map, String name, float defVal) {
        return parseFloat(map.get(name), defVal);
    }

    public static double getAsDouble(Map<String, Object> map, String name) {
        return parseDouble(map.get(name), 0);
    }

    public static double getAsDouble(Map<String, Object> map, String name, double defVal) {
        return parseDouble(map.get(name), defVal);
    }

    public static boolean getAsBool(Map<String, Object> map, String name) {
        return parseBool(map.get(name), false);
    }

    public static boolean getAsBool(Map<String, Object> map, String name, boolean defVal) {
        return parseBool(map.get(name), defVal);
    }

    @SuppressWarnings("unchecked")
    public static List<Object> getAsList(Map<String, Object> map, String name) {
        Object o = map.get(name);
        if(o == null) {
            return null;
        }

        if(o instanceof List<?>) {
            return (List<Object>)o;
        }

        return null;
    }

    @SuppressWarnings("unchecked")
    public static List<String> getAsStrList(Map<String, Object> map, String name) {
        Object o = map.get(name);
        if(o == null) {
            return null;
        }

        List<String> list = new ArrayList<String>();
        if(o instanceof List<?>) {
            for(Object one : (List<Object>)o) {
                list.add(parseString(one, ""));
            }
        }

        return list;
    }

    /**
     * 返回一个string的set，重复的被覆盖
     * @param map
     * @param name
     * @return
     */
    public static Set<String> getAsStrSet(Map<String, Object> map, String name) {
        Object o = map.get(name);
        if(o == null || !(o instanceof List<?>)) {
            return null;
        }

        Set<String> ss = new HashSet<String>();
        List<Object> list = (List<Object>)o;
        for(Object l : list) {
            ss.add(parseString(l, ""));
        }

        return ss;
    }

    public static Map<String, Object> getAsObject(List<Object> list, int i) {
        if(i < 0 || i >= list.size()) {
            return null;
        }
        return (Map<String, Object>)list.get(i);
    }

    public static Map<String, Object> getAsObject(Map<String, Object> map, String name) {
        Object o = map.get(name);
        if(o == null) {
            return null;
        }

        if(o instanceof Map<?, ?>) {
            return (Map<String, Object>)o;
        }

        return null;
    }

    /**
     * 在一个多级的map中获取对象，多级以“.”分隔
     * @param map
     * @param name 对象名称，多级以"."分隔
     * @return
     */
    public static Object getObject(Map<String, Object> map, String name) {
        String[] ss = name.split("\\.");
        int num = ss.length;

        Object o;
        Map<String, Object> subMap = map;

        for(int i = 0; ;){
            o = subMap.get(ss[i]);
            if(o == null) {
                return null;
            }

            if(i == num - 1) {
                return o;
            }

            if(o instanceof Map<?,?>) {
                subMap = (Map<String, Object>)o;
            }
            i++;
        }
    }


}
